package cn.ivast.recyclerviewtest.manager;

import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by hcs on 2018/11/24.
 */
public class TanTanManager extends RecyclerView.LayoutManager {

    public static final int DEFAULT_SHOW_ITEM = 5;
    public static final float DEFAULT_SCALE = 0.1f;
    public static final int DEFAULT_TRANSLATE_Y = 14;

    private RecyclerView recyclerView;
    private ItemTouchHelper itemTouchHelper;

    public TanTanManager(RecyclerView recyclerView,ItemTouchHelper itemTouchHelper) {
        this.recyclerView = recyclerView;
        this.itemTouchHelper = itemTouchHelper;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        //移除所有的View
        removeAllViews();
        //布局之前将所有的子View先Detach掉，放入到Scrap缓存中
        detachAndScrapAttachedViews(recycler);
        int itemCount = getItemCount();
        //当数据源个数大于最大显示数时
        if (itemCount > DEFAULT_SHOW_ITEM) {
            //把数据源倒着循环，这样第0个数据就在屏幕最上面了
            for (int i = DEFAULT_SHOW_ITEM; i >=0 ; i--) {
                View view = recycler.getViewForPosition(i);
                //将Item View加入到RecyclerView中
                addView(view);
                //测量Item View
                measureChildWithMargins(view,0,0);
                //getDecoratedMeasuredWidth(view) 可以得到 Item View 的宽度
                //所以 widthSpace 就是除了 Item View 剩余的值
                int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
                //同理
                int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
                // 将 Item View 放入 RecyclerView 中布局
                // 在这里默认布局是放在 RecyclerView 中心
                layoutDecoratedWithMargins(view,widthSpace/2,heightSpace/2,widthSpace/2+getDecoratedMeasuredWidth(view),heightSpace/2+getDecoratedMeasuredHeight(view));
                // 其实屏幕上有6张卡片，但是我们把第5张和第6张卡片重叠在一起，这样看上去就只有5张
                // 第6张卡片主要是为了保持动画的连贯性
                if (i == DEFAULT_SHOW_ITEM) {
                    //按照一定的规则缩放，并且偏移Y轴
                    // DEFAULT_SCALE 默认为0.1f，DEFAULT_TRANSLATE_Y 默认为14
                    view.setScaleX(1-(i-1)*DEFAULT_SCALE);
                    view.setScaleY(1-(i-1)*DEFAULT_SCALE);
                    view.setTranslationY((i-1)*view.getMeasuredHeight()/DEFAULT_TRANSLATE_Y);
                } else if (i > 0) {
                    view.setScaleX(1 - i * DEFAULT_SCALE);
                    view.setScaleY(1 - i * DEFAULT_SCALE);
                    view.setTranslationY(i * view.getMeasuredHeight() / DEFAULT_TRANSLATE_Y);
                } else {
                    // 设置 mTouchListener 的意义就在于我们想让处于顶层的卡片是可以随意滑动的
                    // 而第二层、第三层等等的卡片是禁止滑动的
                    view.setOnTouchListener(touchListener);
                }
            }
        }else{
            // 当数据源个数小于或等于最大显示数时，和上面的代码差不多
            for (int position = itemCount-1;position>=0 ; position--) {
                View view = recycler.getViewForPosition(position);
                addView(view);
                measureChildWithMargins(view,0,0);
                int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
                int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
                layoutDecoratedWithMargins(view,widthSpace/2,heightSpace/2,widthSpace/2+getDecoratedMeasuredWidth(view),heightSpace/2+getDecoratedMeasuredHeight(view));
                if (position > 0) {
                    view.setScaleX(1 - position * DEFAULT_SCALE);
                    view.setScaleY(1 - position * DEFAULT_SCALE);
                    view.setTranslationY(position * view.getMeasuredHeight() / DEFAULT_TRANSLATE_Y);
                } else {
                    view.setOnTouchListener(touchListener);
                }
            }
        }
    }

    private View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            RecyclerView.ViewHolder childViewHolder = recyclerView.getChildViewHolder(v);
            // 把触摸事件交给 mItemTouchHelper，让其处理卡片滑动事件
            if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                itemTouchHelper.startSwipe(childViewHolder);
            }
            return false;
        }
    };
}
